package cn.anlab.anappframe.net.sub;

import java.io.UnsupportedEncodingException;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Application;
import android.content.Context;

/**
 * 响应分发器
 * 
 * @version 1.0
 * @author Stan Rong 2012-3-6
 */
public class ResponseDispatcher implements IDataHandler {
	
	private static final String DEFAULT_CHARSET_NAME = "UTF-8";
	private static final String CMD_KEY = "cmd";
	private static final String DATA_KEY = "data";
	
	private String charsetName;
	
	public ResponseDispatcher() {
		this.charsetName = DEFAULT_CHARSET_NAME;
	}
	
	public ResponseDispatcher(String charsetName) {
		this.charsetName = charsetName;
	}

	@Override
	public void onDataReceived(byte[] bytes) {
		//转换成字符串
		String content;
		try {
			content = new String(bytes, charsetName);
		} catch (UnsupportedEncodingException e) {
			content = "";
			e.printStackTrace();
		}
		//解析成JSON
		try {
			JSONObject json = new JSONObject(content);
			String cmd = json.getString(CMD_KEY);
			JSONObject data = json.getJSONObject(DATA_KEY);
			dispatchResponse(cmd, json);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 分发响应
	 * @param cmd
	 * @param json
	 */
	private void dispatchResponse(String cmd, JSONObject json) {
		
	}
	
	
	
	
	

}
